﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.LineAlcCodes.Dtos;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.AlcCodes;

namespace KDS.Mes.Mes.LineAlcCodes
{
    /// <summary>
    /// 线体/AlcCode信息服务实现
    /// </summary>
    //[AbpAuthorize(AppPermissions.Pages_Bom_LineAlcCodes)]
    public class LineAlcCodeAppService : MesAppServiceBase, ILineAlcCodeAppService
    {
        private readonly IRepository<LineAlcCode, int> _lineAlcCodeRepository;
        private readonly IRepository<AlcCode, int> _alcCodeRepository;
        private readonly LineAlcCodeManage _lineAlcCodeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public LineAlcCodeAppService(
            IRepository<LineAlcCode, int> lineAlcCodeRepository,
            LineAlcCodeManage lineAlcCodeManage,
            IRepository<AlcCode, int> alcCodeRepository
  )
        {
            _lineAlcCodeRepository = lineAlcCodeRepository;
            _lineAlcCodeManage = lineAlcCodeManage;
            _alcCodeRepository = alcCodeRepository;

        }

        #region 线体/AlcCode信息管理


        /// <summary>
        /// 根据LineId获取对应的AlcCodes
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<ComboboxItemDto> GetAllAlcCodesByLineId(NullableIdDto<int> input)
        {
            List<ComboboxItemDto> output = new List<ComboboxItemDto>();
            //var query = _lineAlcCodeRepository.GetAll()
            //    .Where(u => u.LineId == input.Id).ToList();

            var query = from la in _lineAlcCodeRepository.GetAll()
                        join al in _alcCodeRepository.GetAll() on la.AlcCodeId equals al.Id
                        where la.LineId == input.Id
                        select new
                        {
                            Id=al.Id,
                            AlcCodeName = al.ALCCode
                        };
            output = query.ToList()
               .Select(c => new ComboboxItemDto(c.Id.ToString(), c.AlcCodeName))
               .ToList();
            return output;
        }

        /// <summary>
        /// 根据查询条件获取线体/AlcCode信息分页列表
        /// </summary>
        public async Task<PagedResultDto<LineAlcCodeListDto>> GetPagedLineAlcCodesAsync(GetLineAlcCodeInput input)
        {

            var query = _lineAlcCodeRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var lineAlcCodeCount = await query.CountAsync();

            var lineAlcCodes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var lineAlcCodeListDtos = lineAlcCodes.MapTo<List<LineAlcCodeListDto>>();
            return new PagedResultDto<LineAlcCodeListDto>(
            lineAlcCodeCount,
            lineAlcCodeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取线体/AlcCode信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetLineAlcCodeForEditOutput> GetLineAlcCodeForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetLineAlcCodeForEditOutput();

            LineAlcCodeEditDto lineAlcCodeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _lineAlcCodeRepository.GetAsync(input.Id.Value);
                lineAlcCodeEditDto = entity.MapTo<LineAlcCodeEditDto>();
            }
            else
            {
                lineAlcCodeEditDto = new LineAlcCodeEditDto();
            }

            output.LineAlcCode = lineAlcCodeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取线体/AlcCode信息ListDto信息
        /// </summary>
        public async Task<LineAlcCodeListDto> GetLineAlcCodeByIdAsync(EntityDto<int> input)
        {
            var entity = await _lineAlcCodeRepository.GetAsync(input.Id);

            return entity.MapTo<LineAlcCodeListDto>();
        }







        /// <summary>
        /// 新增或更改线体/AlcCode信息
        /// </summary>
        public async Task CreateOrUpdateLineAlcCodeAsync(CreateOrUpdateLineAlcCodeInput input)
        {
            if (input.LineAlcCodeEditDto.Id.HasValue)
            {
                await UpdateLineAlcCodeAsync(input.LineAlcCodeEditDto);
            }
            else
            {
                await CreateLineAlcCodeAsync(input.LineAlcCodeEditDto);
            }
        }

        /// <summary>
        /// 新增线体/AlcCode信息
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Bom_LineAlcCodes_Create)]
        public virtual async Task<LineAlcCodeEditDto> CreateLineAlcCodeAsync(LineAlcCodeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<LineAlcCode>();

            entity = await _lineAlcCodeRepository.InsertAsync(entity);
            return entity.MapTo<LineAlcCodeEditDto>();
        }

        /// <summary>
        /// 编辑线体/AlcCode信息
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Bom_LineAlcCodes_Edit)]
        public virtual async Task UpdateLineAlcCodeAsync(LineAlcCodeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _lineAlcCodeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _lineAlcCodeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除线体/AlcCode信息
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Bom_LineAlcCodes_Delete)]
        public async Task DeleteLineAlcCodeAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _lineAlcCodeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除线体/AlcCode信息
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Bom_LineAlcCodes_Delete)]
        public async Task BatchDeleteLineAlcCodeAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _lineAlcCodeRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion












    }
}
